<template>
  <div class="home-brand" ref="target">
    <HomePanel title="热门品牌" subTitle="国际经典 品质保证">
      <template #right>
        <a href="javaScript:;" class="iconfont icon-angle-left prev" @click="toggle(-1)" :class="{disabled: index === 0}"></a>
        <a href="javascript:;" class="iconfont icon-angle-right next" @click="toggle(1)" :class="{disabled: index === 1}"></a>
      </template>
      <div class="box" ref="box">
        <ul v-if="brand.length" class="list" :style="{transform: `translateX(${-index*1240}px)`}">
          <li v-for="items in brand" :key="items.id">
            <router-link to="/">
              <img :src="items.picture" alt="">
            </router-link>
          </li>
        </ul>

        <!-- 白屏骨架 -->
        <div v-else class="skeleton">
          <XtxSkeleton width="240px" height="305px" :animated="true" bg="#e4e4e4" v-for="i in 5" :key="i"/>
        </div>
      </div>
    </HomePanel>
  </div>
</template>

<script>
import HomePanel from './home-panel.vue'
import XtxSkeleton from '@/components/library/xtx-skeleton.vue'
import { ref } from 'vue'
import { useLazyData } from '@/hooks'
import { findBrand } from '@/api/home'
export default {
  name: 'HomeBrand',
  components: {
    HomePanel,
    XtxSkeleton
  },
  setup () {
    // 点击后的切换效果，如果为1则下一张，0为上一张
    const index = ref(0)

    // 组件懒加载
    const { target, result } = useLazyData(() => findBrand(10))

    // 上一张下一张逻辑封装
    const toggle = (step) => {
      const newIndex = index.value + step

      // 当超出范围的时候直接返回
      if (newIndex < 0 || newIndex > 1) return

      index.value = newIndex
    }
    console.log(result)
    return { toggle, index, brand: result, target }
  }
}
</script>

<style lang="less" scoped>
.home-panel {
  background: #f5f5f5;

}
.iconfont {
  width: 20px;
  height: 20px;
  background: #ccc;
  color: #fff;
  display: inline-block;
  text-align: center;
  margin-left: 5px;
  background: @xtxColor;
  &::before {
    font-size: 12px;
    position: relative;
    top: -2px;
  }
  &.disabled { // 当上一张或者下一张到达查看的数量时，禁用按钮以及更换鼠标形状
    background: #ccc;
    cursor: not-allowed;
  }
}
.box {
  display: flex;
  width: 100%;
  height: 345px;
  overflow: hidden;
  padding-bottom: 40px;
  .list {
    width: 200%;
    display: flex;
    transition: all 1s;
    li {
      margin-right: 10px;
      width: 240px;
      &:nth-child(5n) {
        margin-right: 0;
      }
      img {
        width: 240px;
        height: 305px;
      }
    }
  }
}

// 白屏骨架样式
.skeleton {
    width: 100%;
    display: flex;
    .item {
      margin-right: 0px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
</style>
